package co.gov.mintic.Biblioteca.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Environment;
import co.gov.mintic.Biblioteca.constants.Constants;

public class Util {

	public static void readData(InputStream is) throws IOException {
		try {
			readInputStream(is);
		} catch (Exception e) {
			System.out.println("Exception:" + e.getMessage());
		}

	}

	/**
	 * Calls an HTTP Get method.
	 * 
	 * @param url
	 *            - URL to call over the Get method.
	 * @param params
	 *            - The list of params required for that URL.
	 * @return Http response List<String>.
	 */
	public static boolean callHTTPGetUpdate(String url) {

		boolean sw = true;

		try {
			URL urlConsulta = new URL(url);
			HttpURLConnection con = (HttpURLConnection) urlConsulta.openConnection();
			readStream(con.getInputStream());

		} catch (Exception e) {
			sw = false;
			System.out.println("Exception:" + e.getMessage());
		} finally {
		}

		return sw;
	}

	/**
	 * 
	 * @param in
	 */
	public static void readInputStream(InputStream inputStream)
			throws IOException {
		File sdCard = null;
		File directory = null;
		OutputStream salida = null;
		try {
			sdCard = Environment.getExternalStorageDirectory();
			directory = new File(sdCard.getAbsolutePath()
					+ Constants.EXTERNAL_DIRECTORY);
			if (!directory.exists())
				directory.mkdirs();

			File file = new File(sdCard.getAbsolutePath()
					+ Constants.EXTERNAL_DIRECTORY + "/"
					+ Constants.EXTERNAL_FILE);
			salida = new FileOutputStream(file);
			byte[] buf = new byte[1024];

			int len;
			while ((len = inputStream.read(buf)) > 0) {
				salida.write(buf, 0, len);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			salida.close();
			inputStream.close();
		}
	}

	/**
	 * 
	 * @param in
	 */
	public static void readStream(InputStream in) {
		BufferedReader reader = null;
		File sdCard = null;
		File directory = null;
		PrintWriter stdOut = null;
		String line = "";

		try {
			reader = new BufferedReader(new InputStreamReader(in));

			sdCard = Environment.getExternalStorageDirectory();
			directory = new File(sdCard.getAbsolutePath()
					+ Constants.EXTERNAL_DIRECTORY);
			if (!directory.exists())
				directory.mkdirs();

			stdOut = new PrintWriter(new FileWriter(sdCard.getAbsolutePath()
					+ Constants.EXTERNAL_DIRECTORY + "/"
					+ Constants.EXTERNAL_FILE));
			while ((line = reader.readLine()) != null) {
				stdOut.println(line);
				stdOut.flush();
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
					stdOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Gets andean encoding.
	 * 
	 * @return default encoding.
	 */
	protected static String getEncoding() {
		return "ISO-8859-1";
	}

	/**
	 * 
	 * Checks if the file exist in the external storage directory
	 * 
	 * @return
	 */
	public static boolean checkFile() {
		File tarjeta = Environment.getExternalStorageDirectory();
		File file = new File(tarjeta.getAbsolutePath()
				+ Constants.EXTERNAL_DIRECTORY, Constants.EXTERNAL_FILE);
		return file.exists();
	}

	/**
	 * 
	 * @param tag
	 * @return
	 */
	public static String getDataFromTag(String tag) {
		String data = "";

		int iniPos = tag.indexOf("[");
		int finPos = tag.indexOf("]");

		data = tag.substring(iniPos + 7, finPos);

		return data.toUpperCase();
	}
}
